package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.SMSUtils;
import com.itheima.common.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/validatecode")
@Slf4j
public class ValidateCodeController {


    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 发送验证码
     * @param telephone
     */
    @PostMapping("/send4Orde")
    public Result send4Orde(String telephone) {
        //生成6位数的随机数
        String code = ValidateCodeUtils.generateValidateCode(6).toString();
        //将验证码存入Redis，时效5分钟
        redisTemplate.opsForValue().set(telephone+"ordercode", code, 5, TimeUnit.MINUTES);
        //SMSUtils.sendMessage(SMSUtils.ORDER_NOTICE, telephone, code);
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }



    @GetMapping("/send4Login")
    public Result send4Login(String telephone) {
        //生成6位数的随机数
        String code = ValidateCodeUtils.generateValidateCode(6).toString();
        //将验证码存入Redis，时效5分钟
        redisTemplate.opsForValue().set(telephone+"logincode", code, 5, TimeUnit.MINUTES);
        //SMSUtils.sendMessage(SMSUtils.ORDER_NOTICE, telephone, code);
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }
}
